﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Utility;
using System.Web;
using System.Configuration;
using System.Web.Configuration;
using System.Web.SessionState;

namespace WebUtility
{
    public class CustomSessionIDManager : SessionIDManager, ISessionIDManager
    {
        private SessionStateSection pConfig = null;
        string ISessionIDManager.GetSessionID(HttpContext context)
        {
            if (pConfig == null)
            {
                var cfg =
                  WebConfigurationManager.OpenWebConfiguration(System.Web.Hosting.HostingEnvironment.ApplicationVirtualPath);
                pConfig = (SessionStateSection)cfg.GetSection("system.web/sessionState");
            }
            string sid = base.GetSessionID(context);//默认从Cookie、UseUri 中获取
            if (string.IsNullOrWhiteSpace(sid)
                && !string.IsNullOrWhiteSpace(context.Request.QueryString[pConfig.CookieName]))
            {
                var _sid = context.Request.QueryString[pConfig.CookieName];//从自定义查询字符中获取，也可以扩展从自定义Header中获取
                return sid;
            }
            return sid;
        }
    }
}
